public class Solution24 {
    public ListNode swapPairs(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode p, q, t;
        p = head;
        q = p.next;
        head = new ListNode();
        head.next = p;
        t = head;

        while (q != null) {
            p.next = q.next;
            q.next = p;
            t.next = q;

            t = p;
            p = p.next;
            if (p == null)
                break;
            q = p.next;
        }
        return head.next;
    }
}
